/*

*/

proc/check_tier(mob/player, mine, max)
	if(mine < max)
		player.client.spell_error("This is not a holy enough place to create this spell!")
		return 0
	return 1

proc/generate_spell(word, tier, player/player)

	. = new/list()

	var
		spell = word
		i = 2
		has_found_element
		tries = length(spell)
		list/required_ink_uses = list()

	while(spell!="" && tries-- >= 0 && i++)

		//check each character at a time
		var/text = copytext(spell, 1, i)

		if(has_found_element)

			//check all of the suffixes
			for(var/suffix/ss in suffixes)


				//we found a match
				if(text == ss.name)

					//verify the tier we are on (inkwise)
					if(!check_tier(player, tier, ss.tier))
						continue

					//reset the counter, and chunk off the current spell
					spell = copytext(spell,i)
					i=1

					//add it to the list to make a spell for
					. += new ss.type
					if(ss.required_ink) required_ink_uses[ss.required_ink]++

			//it is possible to have a dual (or tri) element spell
			for(var/root/ss in roots)

				if(!(ss.name in player.learned)) continue


				if(text == ss.name)

					//verify the tier we are on (inkwise)
					if(!check_tier(player, tier, ss.tier))
						continue

					spell = copytext(spell,i)
					i=1
					. += new ss.type
					if(ss.required_ink) required_ink_uses[ss.required_ink]++
		else
			for(var/prefix/ss in prefixes)

				if(text == ss.name)

					if(!check_tier(player, tier, ss.tier))
						continue

					spell = copytext(spell,i)
					i=1
					. += new ss.type
					if(ss.required_ink) required_ink_uses[ss.required_ink]++

			for(var/root/ss in roots)

				if(!(ss.name in player.learned)) continue

				if(text == ss.name)

					if(!check_tier(player, tier, ss.tier))
						continue

					spell = copytext(spell,i)
					i=1
					. += new ss.type

					//we found a root (which always has an element)
					has_found_element = 1

					if(ss.required_ink) required_ink_uses[ss.required_ink]++


	if(!has_found_element && length(spell)>0)
		player.client.spell_error("You need to have a root element!")

	if(length(spell)>0)
	//	world << "Creation of \"[word]\" has failed."
		. = null

	for(var/o in required_ink_uses)
		if(!(locate(o) in player.ink_vials))
			player.client.spell_error("You are missing some of the required ink for this!")
			.=null

		else
			var/ink/ink = locate(o) in player.ink_vials
			if(ink.uses.get_current() < required_ink_uses[o])
				player.client.spell_error("You do not have enough ink to write this spell!")
				.=null